package com.android.hedgehog;

import java.util.List;

import android.graphics.Color;

import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Input.TouchEvent;
import com.android.hedgehog.framework.Screen;

public class HighScores extends Screen {
	int page = 0;
	int score = 0;
	String lines[] = new String[5];
	String testMsg = "";

	public HighScores(Game game) {
		super(game);

	}

	@Override
	public void update(float deltaTime) {

		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();

		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) {
				if (event.x < 60 && event.y > 420 && page != 0) { // if back
																	// button
					if (Settings.soundEnabled)
						Assets.click.play(1);
					loadCurrentGameScores();
					return;
				}
				if (event.x > 260 && event.y > 420 && page != 14) {// if forward
					if (Settings.soundEnabled)
						Assets.click.play(1);
					score += 10;
					Settings.addScore(1, score);
					return;
				}
				if (event.x > 130 && event.x < 190 && event.y > 420) {// if home
					if (Settings.soundEnabled)
						Assets.click.play(1);
					game.setScreen(new MainMenu(game));
					return;
				}
			}
		}
	}

	private void loadCurrentGameScores() {
		for (int i = 0; i < 5; i++) {
			lines[i] = "" + (i + 1) + ". " + Settings.highScores[page][i];
			testMsg = "name: " + Settings.userName + "  age: " + Settings.age
					+ " gen: " + Settings.isMale + " lev: " + Settings.round;
		}
	}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();

		g.drawPixmap(Assets.blackbackground, 0, 0);
		if (page != 0)
			g.drawPixmap(Assets.whitearrowbuttons, 0, 420, 60, 0, 60, 60);// back
		g.drawPixmap(Assets.whitearrowbuttons, 130, 420, 120, 0, 60, 60);// home
		if (page != 14)
			g.drawPixmap(Assets.whitearrowbuttons, 260, 420, 0, 0, 60, 60);// forward
		for (int i = 0; i < 5; i++) {
			g.drawText(lines[i], 50, 50 + (30 * i), 16, Color.WHITE);
		}
		g.drawText(testMsg, 160, 240, 14, Color.WHITE);

		/*
		 * int y = 100; for (int i = 0; i < 5; i++) { drawText(g, lines[i], 20,
		 * y); y += 50; }
		 */

	}// present()

	public void drawText(Graphics g, String line, int x, int y) {
		int len = line.length();
		for (int i = 0; i < len; i++) {
			char character = line.charAt(i);

			if (character == ' ') {
				x += 20;
				continue;
			}

			int srcX = 0;
			int srcWidth = 0;
			if (character == '.') {
				srcX = 200;
				srcWidth = 10;
			} else {
				srcX = (character - '0') * 20;
				srcWidth = 20;
			}

			g.drawPixmap(Assets.white28fontdigits, x, y, srcX, 0, srcWidth, 32);
			x += srcWidth;
		}
	}

	@Override
	public void pause() {
		Settings.save(game.getFileIO());

	}

	@Override
	public void resume() {

	}

	@Override
	public void dispose() {

	}
}
